package org.openkoala.koala.deploy.tree.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.actions.ActionGroup;
import org.openkoala.koala.deploy.pojo.MethodTree;
import org.openkoala.koala.deploy.webservice.pojo.WebServiceType;

public class WebServiceTreeActionGroup extends ActionGroup {

	private TreeViewer tv;

	private TreeGetAction getAction = new TreeGetAction();

	private TreePostAction postAction = new TreePostAction();
	
	private Action putAction = new TreePutAction();

	private Action deleteAction = new TreeDeleteAction();

	public WebServiceTreeActionGroup(TreeViewer tv) {
		super();
		this.tv = tv;
		putAction.setEnabled(false);
		deleteAction.setEnabled(false);
	}

	@Override
	public void fillContextMenu(IMenuManager mgr) {
		MenuManager menuManager = (MenuManager) mgr; // 类型转换
		// 生成Menu并挂在树Tree上
		Tree tree = tv.getTree();
		Menu menu = menuManager.createContextMenu(tree);
		tree.setMenu(menu);
		mgr.addMenuListener(new IMenuListener() {
			@Override
			public void menuAboutToShow(IMenuManager manager) {
				manager.removeAll();
				TreeItem items[] = tv.getTree().getSelection();
				Object obj = items[0].getData();
				if (obj instanceof MethodTree) {
					MethodTree tree = (MethodTree)obj;
					manager.add(postAction);
					manager.add(getAction);
					if(tree.getType().equals(WebServiceType.GET)){
						postAction.setEnabled(true);
						getAction.setEnabled(false);
					}
					else if (tree.getType().equals(WebServiceType.POST)){
						postAction.setEnabled(false);
						getAction.setEnabled(true);
					}
				}
			}
		});
	}

	private class TreeGetAction extends Action {
		public TreeGetAction() {
			setText("设置为GET");
		}

		public void run() {
			TreeItem items[] = tv.getTree().getSelection();
			Object obj = items[0].getData();
			MethodTree tree = (MethodTree)obj;
			tree.setType(WebServiceType.GET);
			tv.refresh();
		}
	}

	private class TreePostAction extends Action {
		public TreePostAction() {
			setText("设置为Post");
		}

		public void run() {
			TreeItem items[] = tv.getTree().getSelection();
			Object obj = items[0].getData();
			MethodTree tree = (MethodTree)obj;
			tree.setType(WebServiceType.POST);
			tv.refresh();
		}
	}

	private class TreePutAction extends Action {
		public TreePutAction() {
			setText("设置为PUT");
		}

		public void run() {
			MessageDialog.openInformation(null, null, "PUT");
		}
	}

	private class TreeDeleteAction extends Action {
		public TreeDeleteAction() {
			setText("设置为Delete");
		}

		public void run() {
			MessageDialog.openInformation(null, null, "DELETE");
		}
	}
}
